<?php
namespace app\admin\controller;

use think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/19/019
 * Time: 16:19
 */
class Login extends Controller {

    /**
     * 登录页面
     * @return \think\response\View
     */
    public function index() {
        $login = session('login');
        if ($login === 1) {
            $this->redirect(url('index/main'));
        }
        return view();
    }

    /**
     * 验证登录信息
     * @return \think\response\Json
     */
    public function check_login() {
        $param = input('param.');

        if (!isset($param['vercode'])) {
            return json(['code' => 1, 'msg' => '请输入验证码']);
        }

        if( !captcha_check($param['vercode']))
        {
            return json(['code' => 1, 'msg' => '验证码输入错误']);
        }

        if (!isset($param['username'])) {
            return json(['code' => 1, 'msg' => '请输入用户名']);
        }

        if (!isset($param['password'])) {
            return json(['code' => 1, 'msg' => '请输入密码']);
        }

        $accounts = config('accounts.');

        if ($param['username'] != $accounts['username']) {
            return json(['code' => 1, 'msg' => '用户不存在']);
        }

        if (md5($param['password']) != $accounts['password']) {
            return json(['code' => 1, 'msg' => '密码错误']);
        }

        session('login', 1);

        return json(['code' => 0, 'msg' => '登录成功！']);

    }

    /**
     * 退出登录
     * @return \think\response\Json
     */
    public function logout() {
        session('login', 0);
        return json(['code' => 0, 'msg' => '退出成功！']);
    }
}